// pages/orderManager/orderManager.js
const app = getApp()
var api = require('../../utils/networkApi.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    /**
     * 页面配置
     */
    // tab切换
    currentTab: 0,
    //已接单数据
    acceptedOrdersData: [],
    //弹出框是否显示
    showDialog: false,
    //弹出框高度
    phoneNumberpopupWindowsHeight: 0,
    //弹出框弹出高度
    popupWindowsBottomHeight: 0,
    //异常订单数据
    abnormalOrdersData: [],
    //提示弹出框是否显示
    showTipsDialog: false,
    //发单弹出框高度
    dialogHeight: "",
    //弹出框提示内容
    dialogContent: "",
    //弹窗左边按钮
    dialogLeftBtnText: "",
    //弹窗右边按钮
    dialogRightBtnText: "",
    //弹窗标识
    dialogType: "0", //0 异常申请 1 撤销 2 申请撤销
    //弹出框弹出高度
    dialogBottomHeight: "",
    //弹出框标题颜色
    topTitleColor: "#666666",
    //弹出框标题
    topTitle: "提示",
    //弹出框内容左边间距
    contentLeft: "",
    //弹出框内容右边间距
    contentRight: "",
    //弹出框顶部标题是否显示
    isShowTitle: "0", // 0 显示  1  不显示
    //弹出框内容高度
    contentHeight: "100",
    //分页查询
    currentPage: 1,
    allPage: 0,
    //列表点击position
    orderClickPosition: 0,
    //搜索内容
    searchstr: '',
    tabBarHeight: 0,
    refreshing: false,
    nomore: false,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;


    //请求已接单数据
    this.setData({
      acceptedOrdersData: [],
      allPage: 0,
      currentPage: 1
    });
    this.getTrustOrderListPageWpEd();


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {


  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },

  /**
   * 滑动切换tab
   */
  bindChange: function(e) {

    var that = this;
    that.setData({
      currentTab: e.detail.current
    });

    if (e.detail.current == 0) {
      //已接单
      this.setData({
        acceptedOrdersData: [],
        allPage: 0,
        currentPage: 1
      });
      this.getTrustOrderListPageWpEd();
    } else {
      //获取异常数据
      this.setData({
        abnormalOrdersData: [],
        allPage: 0,
        currentPage: 1
      });
      this.getTrustOrderListPageWpEx();
    }

  },
  /**
   * 点击tab切换
   */
  swichNav: function(e) {

    var that = this;

    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  // 多司机电话
  moreDriverPhone: function(e) {
    var that = this;

    that.setData({
      orderClickPosition: e.currentTarget.dataset.index
    })
    if (that.data.acceptedOrdersData[e.currentTarget.dataset.index].wpWayBillResponseList == null) {
      return
    }
  
    //只有一个箱子直接打电话
    if (that.data.acceptedOrdersData[e.currentTarget.dataset.index].wpOrderInfoResponse.goodInfoResponses.length == 1 && that.data.acceptedOrdersData[e.currentTarget.dataset.index].wpOrderInfoResponse.goodInfoResponses[0].containNum == 1) {
      wx.makePhoneCall({
        phoneNumber: that.data.acceptedOrdersData[e.currentTarget.dataset.index].wpWayBillResponseList[0].driverPhone
      })
      return;
    }

    //根据有几个司机，计算弹出框高度
    var popupWindowHeight = (that.data.acceptedOrdersData[e.currentTarget.dataset.index].wpWayBillResponseList.length > 4 ? 4 : that.data.acceptedOrdersData[e.currentTarget.dataset.index].wpWayBillResponseList.length) * 146;
    if (that.data.acceptedOrdersData[e.currentTarget.dataset.index].wayBillStatusEnum == 1) {
      popupWindowHeight = popupWindowHeight + 146;
    }
    console.log("popupWindowHeight:" + popupWindowHeight)

    //根据有几个司机,计算弹出框弹出高度
    var bottomHeight = (app.globalData.screenHeight - popupWindowHeight) / 2
    console.log("bottomHeight:" + bottomHeight)


    that.setData({
      phoneNumberpopupWindowsHeight: popupWindowHeight,
      popupWindowsBottomHeight: bottomHeight
    });

    this.setData({
      showDialog: !this.data.showDialog
    });

  },
  /**
   * 控制 pop 的打开关闭
   * 该方法作用有2:
   * 1：点击弹窗以外的位置可消失弹窗
   * 2：用到弹出或者关闭弹窗的业务逻辑时都可调用
   */
  toggleDialog() {

    if (this.data.showDialog) {
      this.setData({
        phoneNumberpopupWindowsHeight: 0,
        popupWindowsBottomHeight: 0
      });
    }

    this.setData({
      showDialog: !this.data.showDialog
    });

  },
  /**
   *弹窗左边按钮 方法
   */
  dialogLeft() {
    if (this.data.showTipsDialog) {
      this.setData({
        dialogBottomHeight: 0,
      });
    }
    this.setData({
      showTipsDialog: !this.data.showTipsDialog
    });
  },
  /**
   *弹窗右边按钮 方法
   */
  dialogRight() {
    if (this.data.showTipsDialog) {
      this.setData({
        dialogBottomHeight: 0,
      });
    }
    //关闭弹窗
    this.setData({
      showTipsDialog: !this.data.showTipsDialog
    });
    if (this.data.dialogType == "0") {
      //异常信息
      wx.navigateTo({
        url: '/pages/orderManager/abnormalApplication/abnormalApplication?orderClickPosition=' + this.data.orderClickPosition + '&orderItem=' + JSON.stringify(this.data.acceptedOrdersData[this.data.orderClickPosition])
      })
    } else if (this.data.dialogType == "1") {
      //撤销
      var dataBody = {
        "trustOrderNo": this.data.acceptedOrdersData[this.data.orderClickPosition].trustOrderNo,
        "version": this.data.acceptedOrdersData[this.data.orderClickPosition].version
      }
      api.networkForData("backForException", dataBody)
        .then(res => {
          var version = 'acceptedOrdersData[' + this.data.orderClickPosition + '].version';
          var exceptionApplyStatus = 'acceptedOrdersData[' + this.data.orderClickPosition + '].exceptionApplyStatus';
          this.setData({
            [exceptionApplyStatus]: 2,
            [version]: (this.data.acceptedOrdersData[this.data.orderClickPosition].version + 1)
          })
          console.log(res)
        })
    }

  },
  /**
   * 控制 pop 的打开关闭
   * 该方法作用有2:
   * 1：点击弹窗以外的位置可消失弹窗
   * 2：用到弹出或者关闭弹窗的业务逻辑时都可调用
   */
  toggleTipsDialog() {
    if (this.data.showTipsDialog) {
      this.setData({
        dialogBottomHeight: 0,
      });
    }
    this.setData({
      showTipsDialog: !this.data.showTipsDialog
    });

  },
  //去订单详情
  goOrderDetail: function(e) {
    wx.navigateTo({
      url: '/pages/orderManager/orderDetails/orderDetails?orderItem=' + JSON.stringify(this.data.acceptedOrdersData[e.currentTarget.dataset.index])
    })
  },
  //拨打电话
  tel: function(e) {
    wx.makePhoneCall({
      phoneNumber: e.target.dataset.phone,
    })
  },



  //显示异常提示dialog
  showAbnormalTipsDialog: function(e) {

    this.setData({
      dialogHeight: "260",
      orderClickPosition: e.target.dataset.index
    })
    //计算弹出框弹出高度
    var bottomHeight = (app.globalData.screenHeight - this.data.dialogHeight) / 2

    this.setData({
      showTipsDialog: !this.data.showTipsDialog,
      dialogContent: "由于您信息变更，可能会产生额外 费用和法律责任，需要您自行承担",
      dialogLeftBtnText: "取消",
      dialogRightBtnText: "填写信息",
      dialogBottomHeight: bottomHeight,
      //弹出框内容左边间距
      contentLeft: "70",
      //弹出框内容右边间距
      contentRight: "70",
      //弹出框顶部标题是否显示
      isShowTitle: "0", // 0 显示  1  不显示
      dialogType: "0"
    });


  },
  //显示撤销提示dialog
  showRevokeTipsDialog: function(e) {

    this.setData({
      dialogHeight: "260",
      orderClickPosition: e.target.dataset.index
    })
    //计算弹出框弹出高度
    var bottomHeight = (app.globalData.screenHeight - this.data.dialogHeight) / 2

    this.setData({
      showTipsDialog: !this.data.showTipsDialog,
      dialogContent: "撤销后，您可以重新发送异常申请",
      dialogLeftBtnText: "取消",
      dialogRightBtnText: "确认",
      dialogBottomHeight: bottomHeight,
      //弹出框内容左边间距
      contentLeft: "70",
      //弹出框内容右边间距
      contentRight: "70",
      //弹出框顶部标题是否显示
      isShowTitle: "1", // 0 显示  1  不显示
      dialogType: "1"
    });
  },
  //去异常详情
  goAbnormalDetail: function(e) {
    wx.navigateTo({
      url: '/pages/orderManager/abnormalDetail/abnormalDetail?orderItem=' + JSON.stringify(this.data.abnormalOrdersData[e.currentTarget.dataset.index])
    })
  },
  //获取已接单列表数据
  getTrustOrderListPageWpEd: function(ladingNo) {
    var dataBody = {
      "ladingNo": this.data.searchstr,
      "pageData": {
        "page": this.data.currentPage,
        "pageSize": 10
      }
    }
    api.networkForData("getTrustOrderListPageWpEd", dataBody)
      .then(res => {
        this.setData({
          refreshing: false,
          nomore: false,
        });
        this.setData({
          acceptedOrdersData: this.data.acceptedOrdersData.concat(res.list),
          allPage: res.totalPage
        });
        console.log(res)
      })
  },
  //获取异常列表数据
  getTrustOrderListPageWpEx: function() {

    var dataBody = {
      "ladingNo": this.data.searchstr,
      "pageData": {
        "page": this.data.currentPage,
        "pageSize": 10
      }
    }

    api.networkForData("getTrustOrderListPageWpEx", dataBody)
      .then(res => {
        this.setData({
          refreshing: false,
          nomore: false,
        });
        this.setData({
          abnormalOrdersData: this.data.abnormalOrdersData.concat(res.list),
          allPage: res.totalPage
        });
        console.log(res)
      })
  },
  //搜索框输入时触发
  searchList(ev) {
    let e = ev.detail;
    this.setData({
      searchstr: ev.detail.searchstr
    })
  },
  //搜索
  endsearchList() {
    if (this.data.currentTab == 0) {
      //已接单
      this.setData({
        acceptedOrdersData: [],
        allPage: 0,
        currentPage: 1
      });
      this.getTrustOrderListPageWpEd();
    } else {
      //获取异常数据
      this.setData({
        abnormalOrdersData: [],
        allPage: 0,
        currentPage: 1
      });
      this.getTrustOrderListPageWpEx();
    }
  },
  //页面滑动到底部
  loadMore: function() {
    debugger

    this.setData({
      refreshing: true
    })
    if (this.data.currentPage >= this.data.allPage) {
      this.setData({
        refreshing: false,
        nomore: true,
      });
      return;
    } else {
      this.data.currentPage = this.data.currentPage + 1;
      if (this.data.currentTab == 0) {
        //已接单
        this.getTrustOrderListPageWpEd();
      } else {
        //获取异常数据
        this.getTrustOrderListPageWpEx();
      }
    }
  },
  refresh: function() {

    if (this.data.currentTab == 0) {
      //已接单
      this.setData({
        acceptedOrdersData: [],
        allPage: 0,
        currentPage: 1,
        refreshing: true

      });
      this.getTrustOrderListPageWpEd();
    } else {
      //获取异常数据
      this.setData({
        abnormalOrdersData: [],
        allPage: 0,
        refreshing: true,
        currentPage: 1
      });
      this.getTrustOrderListPageWpEx();
    }
  },

})